package com.bocai.eelly.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.activitys.ChoosePhotosActivity;
import com.bocai.eelly.common.BaseFragment;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PostToolsStockFragment extends BaseFragment implements View.OnClickListener{
    @Bind(R.id.et_post_tools_stock_name)
    EditText etPostToolsStockName;
    @Bind(R.id.et_post_tools_stock_brand)
    EditText etPostToolsStockBrand;
    @Bind(R.id.et_post_tools_stock_num)
    EditText etPostToolsStockNum;
    @Bind(R.id.et_post_tools_stock_degree)
    EditText etPostToolsStockDegree;
    @Bind(R.id.et_post_tools_stock_date)
    EditText etPostToolsStockDate;
    @Bind(R.id.et_post_tools_stock_backup)
    EditText etPostToolsStockBackup;
    @Bind(R.id.ll_post_tools_stock_choose_photo)
    LinearLayout llPostToolsStockChoosePhoto;
    @Bind(R.id.tv_post_tools_stock_commit)
    TextView tvPostToolsStockCommit;
    private View view;

    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_post_tools_stock, null);
        initView();
        return view;

    }



    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }
    private void initView() {
        ButterKnife.bind(this, view);
        initEvent();

    }

    private void initEvent() {
        llPostToolsStockChoosePhoto.setOnClickListener(this);
        tvPostToolsStockCommit.setOnClickListener(this);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_tools_stock_choose_photo:
                Intent intent=new Intent(getActivity(),ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.tv_post_tools_stock_commit:

                if (files == null || files.length == 0) {
                    T.showShort(getActivity(), "请选择照片");
                } else {
                    String name = etPostToolsStockName.getText().toString();
                    String brand=etPostToolsStockBrand.getText().toString();
                    String num = etPostToolsStockNum.getText().toString();
                    String degree = etPostToolsStockDegree.getText().toString();
                    String date = etPostToolsStockDate.getText().toString();
                    String backup = etPostToolsStockBackup.getText().toString();
                    if (Utils.isEmpty(name)) {
                        T.showShort(getActivity(), "请输入设备名称");
                    } else if (Utils.isEmpty(brand)) {
                        T.showShort(getActivity(), "请输入设备品牌");
                    } else if (Utils.isEmpty(num)) {
                        T.showShort(getActivity(), "请输入设备数量");
                    } else if (Utils.isEmpty(degree)) {
                        T.showShort(getActivity(), "请输入设备新旧程度");
                    } else if (Utils.isEmpty(date)) {
                        T.showShort(getActivity(), "请输入购买年份");
                    } else if (Utils.isEmpty(backup)) {
                        T.showShort(getActivity(), "请输入其他说明");
                    } else {
                        Loading.show(getActivity(), getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }
    private void commit() {
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_TOOLS_STOCK, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(getActivity(),e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(getActivity(), response);
                }
            },files,fileKeys,getParams(etPostToolsStockName.getText().toString(),etPostToolsStockBrand.getText().toString(),etPostToolsStockNum.getText().toString(),
                    etPostToolsStockDegree.getText().toString(),etPostToolsStockDate.getText().toString(), etPostToolsStockBackup.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public OkHttpClientManager.Param[] getParams(String title, String brand, String num, String degree, String buydate, String backup){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(getActivity(),"id")),
                new OkHttpClientManager.Param("title", title),
                new OkHttpClientManager.Param("brand", brand),
                new OkHttpClientManager.Param("num", num),
                new OkHttpClientManager.Param("degree", degree),
                new OkHttpClientManager.Param("buydate", buydate),
                new OkHttpClientManager.Param("backup", backup),
        };
    }
}
